import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);
var url = 'http://jx2.vaiwan.com/web/'
// var url = '/ZHU'
var url2 = '/api'
// var url3 = 'http://39.99.130.119:9999/api' //测试
var url3 = '/api' //测试
var url4 = 'http://wz-wood.caizhiku.cn'
export default new Vuex.Store({
  state: {
    constantRoutes: [{
        path: '/',
        name: '登录',
        component: () => import(`@/components/page/login`),
        meta: {
          historyRoute: false,
          topTabs: false,
          leftNav: false,
        }
      },
      {
        path: '/contract/List',
        name: '退款订单管理',
        component: () => import(`@/components/page/contract/List`),

      },
      {
        path: '/payOrder/List',
        name: '支付订单',
        component: () => import(`@/components/page/payOrder/List`),

      },
      {
        path: '/withdrawOrder/List',
        name: '提现订单',
        component: () => import(`@/components/page/withdrawOrder/List`),

      },
      {
        path: '/riskRules/List',
        name: '业务风控规则',
        component: () => import(`@/components/page/riskRules/List`),

      },
      {
        path: '/IPBlack/List',
        name: 'IP黑名单',
        component: () => import(`@/components/page/IPBlack/List`),

      },
      {
        path: '/upMerchant/List',
        name: '上游商户',
        component: () => import(`@/components/page/upMerchant/List`),

      },
      {
        path: '/downMerchant/List',
        name: '下游商户',
        component: () => import(`@/components/page/downMerchant/List`),

      },

    ],
    count: 1,
    token: '',
    loadingShow: false, //加载动画
    // http://jx.vaiwan.com/web/
    baseUrl: process.env.NODE_ENV == 'development' ? url3 : url3,
    openUrl: process.env.NODE_ENV == 'development' ? url4 : '', //本地打开木材老数据用域名
    newRoutes: [], //动态路由数组
    menuList: [

    ], //导航栏菜单
    thirdMenu: [], //三级路由
    typeAllList: [], //全部基础数据列表
    operateShow: true, //列表的操作列的显示

    meanTotal: {}, //设置菜单待确认数量
  },
  mutations: {
    /**
     * @desc 设置路由数组
     * @param  newRoutes 新动态路由 
     * @param  menuList 导航栏菜单 
     */
    SET_MENULIST(state, res) {
      state.newRoutes = res.newRoutes
      state.menuList = res.menuList
    },

  },
  actions: {
    /**
     * @desc 设置动态路由数组
     * @param 
     * 数据库配置路由注意事项
     * 一级路由只用配置路由名和id
     * 
     * 二级路由需要id name path component path代表vue访问路径 
     * component路由模板代表在page下的页面路径
     * 
     * 各个信息页面和标签栏需要归为三级路由 只有三级路由的 hiden_in_menu 是有效的
     */
    SET_MENULIST({
      commit
    }, marketInfo) {
      var menuList = window.localStorage.menuList ? JSON.parse(window.localStorage.menuList) : []
      var accessRoutes = [] //路由数组
      console.log('menuList', menuList)
      menuList.map((item, index) => {
        //一级路由有子路由的不分配路由地址
        if (item.childList.length == 0) { //一级路由
          accessRoutes.push({
            path: item.path,
            name: item.name,
            meta: {
              keepAlive: item.keepAlive,
            },
            component: (resolve) => require([`@/components/page${item.component}`], resolve)

          })
        } else {
          //二级路由有无子路由都分配地址 
          item.childList.map((item2, index2) => { //二级路由
            accessRoutes.push({
              path: item2.path,
              name: item2.name,
              meta: {
                keepAlive: item2.keepAlive,
              },
              component: (resolve) => require([`@/components/page${item2.component}`], resolve)
            })
            if (item2.childList) {

              if (item2.childList.length == 0) {
                // console.log('accessRoutes1', accessRoutes)
              } else {
                //三级路由有无子路由都分配地址 
                item2.childList.map((item3, index3) => { //三级路由
                  accessRoutes.push({
                    path: item3.path,
                    name: item3.name,
                    meta: {
                      keepAlive: item3.keepAlive,
                    },
                    component: (resolve) => require([`@/components/page${item3.component}`], resolve)
                  })
                })
              }
            }
          })
        }
      })
      console.log('accessRoutes', accessRoutes)
      accessRoutes.push({ //最后添加404页面 防止页面刷新空白
        path: '*',
        component: (resolve) => require([`@/components/page/notFound`], resolve),
        meta: {
          historyRoute: false,
          topTabs: false,
          leftNav: false,
        }
      }, )

      commit({
        type: "SET_MENULIST",
        newRoutes: accessRoutes,
        menuList: menuList
      });
    },

  },
})
